package com.group.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group.pojo.Product;
import com.group.pojo.ProductCategory;
import com.group.pojo.User;
import com.group.service.ProductCategoryService;
import com.group.service.ProductService;
import com.group.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author group
 * @since 2021-09-06
 */
@RestController
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;
    @Autowired
    private ObjectMapper jackson;

    @RequestMapping("selectProductCategoryList1")
    public R selectProductCategoryList1(@RequestBody Map<String, Object> map) throws JsonProcessingException {
        ProductCategory productCategory = jackson.readValue(jackson.writeValueAsString(map.get("productCategory")), ProductCategory.class);
        Integer pageNo = (Integer) map.get("pageNo");
        Integer pageSize = (Integer) map.get("pageSize");
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 5 : pageSize;
        PageHelper.startPage(pageNo, pageSize);
        List<ProductCategory> list = productCategoryService.selectProductCategoryList1(productCategory);
        PageInfo<ProductCategory> pageInfo = new PageInfo<>(list);
        return R.ok().data("pageinfo", pageInfo);
    }
    @RequestMapping("getCategory1")
    public R getCategory1(){
        List<ProductCategory> productCategories = productCategoryService.selectProductCategoryList1(null);
        return R.ok().data("category",productCategories);
    }

    @RequestMapping("addProductCategory1")
    public R addProductCategory1(@RequestBody ProductCategory productCategory) {
        boolean flag = productCategoryService.addProductCategory1(productCategory);

        return flag ? R.ok() : R.error();
    }

    @GetMapping("deleteProductCategory1")
    public R deleteProductCategory1(String ids) {
        String idstr[] = ids.split(",");

        boolean flag = productCategoryService.deleteProductCategory1(idstr);

        return flag ? R.ok() : R.error();
    }

    @RequestMapping("updateProductCategory1")
    public R updateProductCategory1(@RequestBody ProductCategory productCategory) {
        boolean flag = productCategoryService.updateProductCategory1(productCategory);
        return flag ? R.ok() : R.error();
    }
}

